
Yacon.widget.grid.list.HeadCell = function (conText)
{
	this.fheadRow        = null;
	
	Yacon.apply(this,conText);
	
	this.create();
}
Yacon.widget.grid.list.HeadCell.Extend(Yacon.widget.WinControl,{
	
	create:function()
	{
		if (this.isRowBar())
		{
			var pos = this.getAbsPos();
			this.setStyle("position","absolute");
			this.setStyle("left", pos[0] + pos[2]);
		}
		
		
		/*alert(this.getAttr("fieldName"));*/
		
		this.setFieldName(this.getAttr("fieldName"));
		
		this.element.Control = this;
	},
	
	isFieldVisible:function()
	{	
		if(this.element){			
			if(Yacon.isBootstrap()){
				return Yacon.isTrue(this.getAttr("fieldvisible"));
			}else{
				return Yacon.isTrue(this.element.getAttribute("fieldVisible"));
			}		
		}		
		else{
			return false;
		}			
	},
	isFieldNull:function()
	{
		if(this.element)
			return Yacon.isTrue(this.element.getAttribute("fieldNull"));
		else
			return false;
	},
	isImeMode:function()
	{
		if(this.element)
			return Yacon.isTrue(this.element.getAttribute("imeMode"));
		else
			return false;
	},
	isEditDisabled:function()
	{
		if(this.element)
			return Yacon.isTrue(this.element.getAttribute("editDisabled"));
		else
			return false;
	},
	isInsertDisabled:function()
	{
		if(this.element)
			return Yacon.isTrue(this.element.getAttribute("insertDisabled"));
		else
			return false;
	},
	isRowBar:function()
	{
		if(this.element)
			return Yacon.isTrue(this.element.getAttribute("isRowBar"));
		else
			return false;
	},
	isleftTitle:function()
	{
		if(this.element)
			return Yacon.isTrue(this.element.getAttribute("isleftTitle"));
		else
			return false;
	},
	getFieldAlign:function()
	{
		if(this.element)
			return this.element.getAttribute("fieldAlign");
		else
			return "";
	},
	getFieldType:function()
	{
		if(this.element)
			return this.element.getAttribute("fieldType");
		else
			return "text";
	},
	getMaxLength:function()
	{
		if(this.element)
			return Yacon.getInt(this.element.getAttribute("maxLength"));
		else
			return 0;
	},
	getDecLength:function()
	{
		if(this.element)
			return Yacon.getInt(this.element.getAttribute("decLength"));
		else
			return 0;
	},
	getMaxValue:function()
	{
		if(this.element)
			return this.element.getAttribute("maxValue");
		else
			return "";
	},
	getMinValue:function()
	{
		if(this.element)
			return this.element.getAttribute("minValue");
		else
			return "";
	},	
	getWidth:function()
	{
		if(this.element)
			return this.element.offsetWidth;
		else
			return 0;
	},
	setWidth:function(width)
	{
		if(this.element)
		{
			this.element.style.width = width+"px";
			this.element.width = width+"px";
		}
	},
	getHeight:function()
	{
		if(this.element)
			return this.element.offsetHeight;
		else
			return 0;
	},
	getRowBar:function()
	{
		if(!this.rowBarStr)
		{
			this.rowBarStr = this.element.getAttribute("barStr").decode();
			return this.rowBarStr;
		}else
			return this.rowBarStr;
	},
	getDropdownExp:function()
	{
		if(this.element)
			return this.element.getAttribute("dropdownexp");
		else
			return "";
	}
});

Yacon.widget.grid.list.DataCell = function (conText)
{
	this.fDataRow     = null;
	this.isNewObj     = false;
	this.fdataField   = null;
	this.headCell     = null;
	
	Yacon.apply(this,conText);
	
	this.create();
}

Yacon.widget.grid.list.DataCell.Extend(Yacon.widget.WinControl,{
		
	create:function()
	{
		if (this.headCell)
			this.setFieldName(this.headCell.getFieldName());
		if (this.element)
		{
			this.element.Control = this;
		}
	},
	setElement:function(element)
	{
		
		this.element = element;
		if (this.element)
			this.element.Control = this;
		
	},
	getFieldValue:function()
	{
		var fvalue = "";
		if(this.element)
			fvalue = this.element.getAttribute("fieldValue");
		
		if (Yacon.isEmpty(fvalue))
			fvalue = "";
		return fvalue;
	},
	getFieldText:function()
	{
		var fvalue = "";
		if(this.element)
			fvalue = this.element.getAttribute("innerText");
		
		if (Yacon.isEmpty(fvalue))
			fvalue = "";
		return fvalue;
	},
	
	setCellValue:function(fieldValue,fieldText)
	{
		if(this.element)
		{
			this.element.setAttribute("fieldValue",fieldValue);
			this.element.innerText = fieldText;
		}
	},	
	setCellIndex:function(cellIndex)
	{
		this.cellIndex = cellIndex;
	},	
	/**
	 * 赋值表头单元对象
	 */
	setHeadCell:function(headCell)
	{
		this.headCell  = headCell;
	},
	
	/**
	 * 采用创建对象还是采用模板创建对象
	 */
	setNewObj:function(isNewObj)
	{
		this.isNewObj  = isNewObj;
	},
	/**
	 * 赋值数据集字段对象
	 */
	setDataField:function(dataField)
	{
		this.dataField  = dataField;
	},

	setWidth:function(width)
	{
		this.element.width = width+"px";
		this.setStyle("width",width+"px");
	},
	getWidth:function()
	{
		if(this.element)
			return this.element.offsetWidth;
		else
			return 0;
	},
	changeFieldWidth:function()
	{
		if (this.headCell)
			this.setWidth(this.headCell.getWidth());
	},
	setHeight:function(height)
	{
		this.setStyle("height",height+"px");
	},
	getHeight:function()
	{
		if(this.element)
			return this.element.offsetHeight;
		else
			return 0;
	},
	createTemplate:function()
	{
		if (this.headCell.isleftTitle())
			return this.createLeftTemplate();
		else
			return this.createCustomTemplate();
		
	},
	createCustomTemplate:function()
	{
		this.dataField = this.parent.record.getField(this.fieldName);
		if (!this.dataField)
			return "";
		
		var cellTem = "<td vAlign='center' noWrap=true align='"+this.headCell.getFieldAlign()+"' ";
	
		if (this.headCell.isFieldVisible())
		{
			cellTem = cellTem +" style='width:" + this.headCell.getWidth() +";'";
		}else
		{
			cellTem = cellTem +" style='display:none;' ";
		}
		cellTem = cellTem +" onclick='Yacon.widget.grid.list.DataCell.events.onCellClick()' fieldName='"+this.fieldName+"'";	
		
		if (this.headCell.isRowBar())
		{
			cellTem = cellTem +" class='rowbar' fieldValue='"+this.dataField.getFieldValue()+"'>" +this.fheadCell.getRowBar() +"</td>";
		}else
			cellTem = cellTem +" fieldValue='"+this.dataField.getFieldValue()+"'>"+this.dataField.getFieldText()+"</td>";
		
		return cellTem;
	},
	createLeftTemplate:function()
	{
		var cellTem = "<td class='leftTitle' style='width:" + this.headCell.getWidth() +";' align ='center'>"
	
		if (this.parentList.isCheck())
		{
			cellTem = cellTem + "<input onclick='Yacon.widget.grid.list.BaseGrid.events.onChoiceRowClick(this)' type='checkbox' style='borderStyle:none;' id='data_choice_"+this.parentList.elementID+"'/>";
		}
		
		cellTem = cellTem + "</td>";
		
		return cellTem;
	}	
});

Yacon.widget.grid.list.DataCell.events = {
	onCellClick:function()
	{
		var cellObj   =  Yacon.getTD(event.srcElement);
		var rowObj    =  Yacon.getTR(cellObj);	
		
		if (!rowObj.Control)
			return;
			
		rowObj.Control.setActiveCell(cellObj);
		
		if (Yacon.isfireuserEvent(rowObj.Control.parentList.elementID+"_onCellClick"))
		{
			Yacon.fireUserEvent(rowObj.Control.parentList.elementID+"_onCellClick",[tableObj.Control.parentList]);
		}
	}
}

Yacon.widget.grid.list.EditCell = function (conText)
{
	Yacon.apply(this,conText);
	
	this.create();
	
	if (this.element)
		this.element.Control = this;
}

Yacon.widget.grid.list.EditCell.Extend(Yacon.widget.grid.list.DataCell,{
	
	isCellEdit:function()
	{
		var isCellEdit = false;
		if (this.parentList.getDataSet().isEdit() && !this.headCell.isEditDisabled())
			isCellEdit = true;		
		else if (this.parentList.getDataSet().isInsert()&& !this.headCell.isInsertDisabled())
			isCellEdit = true;
		
		return isCellEdit;
	},	
	activeCell:function()
	{
		if (!this.isCellEdit())
			return;
		
		this.addAttr("dataSet",this.parentList.getDataSet().getID());
		this.addAttr("fieldNull",this.headCell.isFieldNull());
		this.addAttr("fieldType",this.headCell.getFieldType());
		this.addAttr("maxLength",this.headCell.getMaxLength());
		this.addAttr("decLength",this.headCell.getDecLength());
		this.addAttr("maxValue",this.headCell.getMaxValue());
		this.addAttr("minValue",this.headCell.getMinValue());
		this.addAttr("imeMode",this.headCell.isImeMode());
		this.addAttr("fieldName",this.headCell.getFieldName());
		this.element.headCell = this.headCell;		
		
		if (Yacon.isfireuserEvent(this.headCell.getFieldName() + "_beforeEdit"))
		{
			Yacon.fireUserEvent(this.headCell.getFieldName() + "_beforeEdit",[this.activeCell]);
		}
		this.activeElement();
		
	},
	activeElement:function ()
	{
		var fieldType = this.element.getAttribute("fieldType");
		if (fieldType.isEqual(Yacon.widget.FIELD_TEXT)
				||fieldType.isEqual(Yacon.widget.FIELD_INT) 
				||fieldType.isEqual(Yacon.widget.FIELD_NUMBER)
			  )
		{
			if (!window.input)
				window.input = new Yacon.widget.Input({attach:this.element});
			
			window.input.attachElement(this.element);
			
		}else if (fieldType.isEqual(Yacon.widget.FIELD_DROP_SELECT))
		{
			if (this.element.headCell)
			{
				if (!this.element.headCell.drop)
				{
					this.element.headCell.drop = new Yacon.widget.Drop({attach:this.element});
				}
			
				this.element.headCell.drop.attachElement(this.element);				
			
			}else
			{
				var eleObj = this.element;
				
				if (!this.element)
					this.element = new Yacon.widget.Drop({attach:this.element});
				
				this.element.attachElement(this.element);
			}
			
		}else if (fieldType.isEqual(Yacon.widget.FIELD_IP))
		{
			if (!window.inputIP)
				window.inputIP = new Yacon.widget.InputIP({attach:this.element});
			
			window.inputIP.attachElement(this.element);
		}			
	},
	createCell:function()
	{
		if (this.headCell.isleftTitle())
			return this.createLeftCell();
		else
			return this.createCustomCell();
		
	},
	createCustomCell:function()
	{
		this.element   = this.parent.element.insertCell(this.headCell.cellIndex);
		this.element.vAlign = "center";
		if (this.headCell.isFieldVisible())
		{
			this.setWidth(this.headCell.getWidth());
		}else
		{
			this.setStyle("display","none");
		}
		this.element.noWrap = "true";
		this.element.align = this.headCell.getFieldAlign();
		this.addAttr("fieldName",this.headCell.getFieldName());
		
		if (this.headCell.isRowBar())
		{
			this.element.className = "rowbar";
			this.element.innerHTML = this.headCell.getRowBar();
		}		
		this.addEvents("click",Yacon.widget.grid.list.DataCell.events.onCellClick);
		this.element.Control = this;
		return this.element;
	},
	createLeftCell:function()
	{
		this.element    = this.parent.element.insertCell(this.headCell.cellIndex);
		this.element.className = "leftTitle";
		this.element.align ="center";
		this.setWidth(this.headCell.getWidth());
		
		if (this.parentList.isCheck())
		{
			var checkbox = document.createElement("input");
			checkbox.type = "checkbox";
			checkbox.style.borderStyle = "none";
			checkbox.id="data_choice_"+this.parentList.elementID;
			
			this.element.innerHTML=checkbox.outerHTML;
		}		
		this.element.Control = this;
		return this.element;
	}
	
});




